package com.ai.sml.util;

import java.sql.Timestamp;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;

public class DateOper {
	/**
	 * 默认时间格式
	 */
	private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 定义时间格式1
	 */
	private final static String DATE_FORMAT1 = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 日志输出
	 */
	private static Logger logger = Logger.getLogger(DateOper.class);

	/**
	 * 
	 * 获取当前年份
	 * 
	 * @author 
	 * @date 
	 * @version: V1.0
	 * 
	 * @return 当前年份
	 */
	public static String getCurYear() {
		Calendar cal = Calendar.getInstance();
		return String.valueOf(cal.get(1));
	}

	/**
	 * 
	 * 获取当前月份（两位）
	 * 
	 * @author 
	 * @date 2012-6-2 上午11:52:44
	 * @version: V1.0
	 * 
	 * @return 当前月份
	 */
	public static String getCurMonth() {
		Calendar cal = Calendar.getInstance();

		int curMonth = cal.get(Calendar.MONTH) + 1;

		String curMonthStr;

		if (curMonth < 10)// 这里要小于9，因为如果为9时，加1就变为10，这时不用再变两位
		{
			curMonthStr = "0" + curMonth;
		} else {
			curMonthStr = String.valueOf(curMonth);
		}

		return curMonthStr;
	}

	/**
	 * 
	 * 获取指定年月份的最大天数
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param yearmonth
	 *            年月串（yyyyMM）
	 * @return 最大天数
	 */
	public static String getMaxDay(String yearmonth) {

		String tmp = evalTime(yearmonth, "yyyyMM", "yyyyMM", "month", 1);
		String tmp2 = tmp + "01";
		String tmp3 = evalTime(tmp2, "yyyyMMdd", "dd", "day", -1);
		return tmp3;
	}

	/**
	 * 
	 * 获取下月月初日期串，格式yyyyMMdd
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @return 返回下月月初日期字符串（yyyyMMdd）
	 */
	public static String getNextMonthBeginDay() {
		String nextYear;
		String nextMonth;
		String nextDay = "01";

		int curYear;
		int curMonth;

		Calendar cal = Calendar.getInstance();

		curYear = cal.get(Calendar.YEAR);
		curMonth = cal.get(Calendar.MONTH);

		if (curMonth + 1 == 12) // 当月为12月时，下月应该为下一年的1月，年份应该加1
		{
			nextYear = String.valueOf(curMonth + 1);
			nextMonth = "01";
		} else {
			nextYear = String.valueOf(curYear);

			if (curMonth + 2 < 10) // 当下月为小于10月时，应该为下月的前面加0，如02到09
			{
				nextMonth = "0" + (curMonth + 2);
			} else {
				nextMonth = String.valueOf(curMonth + 2);
			}
		}

		return nextYear + nextMonth + nextDay;
	}

	/**
	 * 
	 * 获得上一个月的月份Calendar对象
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param cal
	 * @return 上月月份对象Calendar
	 */
	public static Calendar getPreviousMonth(Calendar calIn) {
		Calendar cal = calIn;
		if (calIn == null) {
			cal = Calendar.getInstance();
		}
		cal.add(Calendar.MONTH, -1);// 减一个月，变为上一月

		return cal;
	}

	/**
	 * 
	 * 当前时间换为时间戳java.sql.Timestamp
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @return Timestamp时间戳对象
	 * @throws ParseException
	 *             异常
	 */
	public static Timestamp getCurTimestamp() throws ParseException {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 
	 * 日期字符串转换为时间戳java.sql.Timestamp
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param date
	 *            被转换日期字符串
	 * @param format
	 *            日期格式
	 * @return Timestamp时间戳对象
	 * @throws ParseException
	 *             异常
	 */
	public static Timestamp string2Timestamp(String date, String format) throws ParseException {
		Date d = new SimpleDateFormat(format, Locale.getDefault()).parse(date);

		return new java.sql.Timestamp(d.getTime());
	}

	/**
	 * 
	 * 比较两个日期的大小
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param date1
	 *            日期参数1
	 * @param date2
	 *            日期参数2
	 * @return 如果date1大于date2，返回true，否则返回false
	 */
	public static boolean compare(Date date1, Date date2) {
		if (date1.after(date2)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * 比较两个日期字符串的大小
	 * 
	 * @author 
	 * @date 2012-6-26 下午2:33:59
	 * @version: V1.0
	 * 
	 * @param date1
	 *            日期参数1
	 * @param format1
	 *            日期格式1 默认格式：yyyy-MM-dd
	 * @param date2
	 *            日期参数2
	 * @param format2
	 *            日期格式2 默认格式：yyyy-MM-dd
	 * @return 如果date1大于date2，返回true，否则返回false
	 * @throws ParseException
	 *             异常
	 */
	public static boolean compare(String date1, String format1In, String date2, String format2In) throws ParseException {
		String format1 = format1In,format2 = format2In;
		if (null == format1In || "".equals(format1In)) {
			format1 = "yyyy-MM-dd";
		}
		if (null == format2In || "".equals(format2In)) {
			format2 = "yyyy-MM-dd";
		}

		Date formatDate1 = (Date) new SimpleDateFormat(format1).parse(date1);

		Date formatDate2 = (Date) new SimpleDateFormat(format2).parse(date2);

		if (formatDate1.after(formatDate2)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * 将指定时间转为字符串格式（默认格式：yyyy-MM-dd HH:mm:ss）
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param date
	 *            被转换的时间对象
	 * @return 转换后的字符串
	 */
	public static String date2String(Date date) {
		return new SimpleDateFormat(DATE_FORMAT1).format(date);
	}

	/**
	 * 
	 * 根据指定格式，将日期时间转换为字符串（默认格式：yyyy-MM-dd HH:mm:ss）
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param date
	 *            被转换的时间对象
	 * @param format
	 *            转换后格式，若format为null或""，则取默认格式yyyy-MM-dd HH:mm:ss
	 * @return 转换后的字符串，若date为null，则返回""
	 */
	public static String date2String(Date date, String formatIn) {
		String format = formatIn;
		if (null == date) {
			return "";
		}
		if (null == formatIn || "".equals(formatIn)) {
			format = DATE_FORMAT1;
		}

		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * 
	 * 日期格式转化
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param srcDate
	 *            源日期字符串
	 * @param srcFormat
	 *            源日期格式，默认为yyyy-MM-dd
	 * @param destFormat
	 *            目的日期格式，默认为yyyy-MM-dd
	 * @return 转换格式后的日期字符串。当srcDate为null或 空字符串时，则返回空字符串；当srcDate解析错误时，则返回当前日期字符串
	 */
	public static String dateFormat(String srcDate, String srcFormatIn, String destFormatIn) {
		String srcFormat = srcFormatIn, destFormat = destFormatIn;
		if (null == srcFormatIn || "".equals(srcFormatIn)) {
			srcFormat = "yyyy-MM-dd";
		}
		if (null == destFormatIn || "".equals(destFormatIn)) {
			destFormat = "yyyy-MM-dd";
		}

		if (null == srcDate || "".equals(srcDate)) {
			return "";
		}

		Calendar cal = Calendar.getInstance();

		Date srcFormatDate = null;

		try {
			srcFormatDate = (Date) new SimpleDateFormat(srcFormat).parse(srcDate);
		} catch (ParseException e) {
			logger.error("", e);
			srcFormatDate = new Date();
		}

		cal.setTime(srcFormatDate);

		return new SimpleDateFormat(destFormat).format(cal.getTime());

	}

	/**
	 * 
	 * 将当前时间转为字符串格式（默认格式：yyyy-MM-dd HH:mm:ss）
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @return 转换后的字符串
	 */
	public static String now2String() {
		return new SimpleDateFormat(DATE_FORMAT1).format(new Date());
	}

	/**
	 * 
	 * 将当前时间转为字符串格式（默认格式：yyyy-MM-dd HH:mm:ss）
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param format
	 *            转换后格式，若format为null或""，则取默认格式yyyy-MM-dd HH:mm:ss
	 * @return 转换后的字符串
	 */
	public static String now2String(String formatIn) {
		String format = formatIn;
		if (null == formatIn || "".equals(formatIn)) {
			format = DATE_FORMAT1;
		}
		return new SimpleDateFormat(format).format(new Date());
	}

	/**
	 * 
	 * 字符串日期转化为java.util.Date（字符串格式:yyyy-MM-dd HH:mm:ss）
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param srcDate
	 *            源日期字符串
	 * @return 转换后的Date对象，若出现异常，则返回当前日期
	 */
	public static Date string2Date(String date) {
		if (date == null || "".equals(date)) {
			return new Date();
		}

		try {
			return new SimpleDateFormat(DATE_FORMAT1).parse(date);
		} catch (ParseException e) {
			logger.error(e);
		}

		return new Date();
	}

	/**
	 * 
	 * 字符串日期转化为java.util.Date
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param srcDate
	 *            源日期字符串
	 * @param srcFormat
	 *            源日期格式，默认为yyyy-MM-dd HH:mm:ss
	 * @return 转换后的Date对象。如果srcDate为空或异常，则返回当前日期
	 */
	public static Date string2Date(String date, String formatIn) {
		String format = formatIn;
		if (null == date || "".equals(date)) {
			return new Date();
		}
		if (null == formatIn || "".equals(formatIn)) {
			format = DATE_FORMAT1;
		}

		Calendar cal = Calendar.getInstance();

		Date currentTime = null;

		try {
			currentTime = (Date) new java.text.SimpleDateFormat(format).parse(date);
		} catch (ParseException e) {
			logger.error("", e);

			return new Date();
		}

		cal.setTime(currentTime);

		return cal.getTime();
	}

	/**
	 * 
	 * java.util.Date转化为java.sql.Date
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param date
	 *            被转换的日期对象
	 * @return 转换后的日期对象, 若入参为null，则返回null
	 */
	public static java.sql.Date date2SqlDate(Date date) {
		if (null == date) {
			return null;
		}

		return new java.sql.Date(date.getTime());
	}

	/**
	 * 
	 * java.util.Date转化为java.sql.Timestamp
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param date
	 *            被转换的日期对象
	 * @return 转换后的时间戳对象, 若入参为null，则返回null
	 */
	public static java.sql.Timestamp date2Timestamp(Date date) {
		if (null == date) {
			return null;
		}

		return new java.sql.Timestamp(date.getTime());
	}

	/**
	 * 
	 * 将字符串转换为java.sql.Date对象
	 * 
	 * @author 
	 * @date 2012-6-4 上午10:44:10
	 * @version: V1.0
	 * 
	 * @param strDate
	 *            原日期串
	 * @param pattern
	 *            转换格式
	 * @return 转换后的日期对象, 若入参为null，则返回null
	 * @throws ParseException
	 */
	public static java.sql.Date string2SqlDate(String date, String format) throws ParseException {
		if (date == null || format == null || "".equals(date) || "".equals(format)) {
			return null;
		}

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

		Date formatDate = simpleDateFormat.parse(date);

		return new java.sql.Date(formatDate.getTime());
	}

	/**
	 * 
	 * 判断是否是闰年
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param year
	 * @return true-year是闰年，false-year不是闰年
	 */
	public static boolean isLeapyear(int year) {
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * 判断当前时间是否在两个时间点之间，时间格式：HH:mm:ss
	 * 
	 * @author 
	 * @date
	 * @version: V1.0
	 * 
	 * @param time1
	 *            起始时间串(HH:mm:ss)
	 * @param time2
	 *            终止时间串(HH:mm:ss)
	 * @return true-当前时间在tiem1与time2之间，false-当前时间不在tiem1与time2之间
	 */
	public static boolean checkTimeShort(String time1, String time2) {
		/**
		 * 时间是否跨天，true-是，false-否
		 */
		boolean flag = false;

		// 若开始时间>结束时间，则表示时间跨天
		if (time1.compareTo(time2) > 0) {
			flag = true;
		}

		// 当前时间
		Calendar calendar = Calendar.getInstance();
		Date defaultDate = calendar.getTime();

		Date date11 = string2Date(date2String(defaultDate, "yyyy-MM-dd") + " " + time1, DEFAULT_PATTERN);// 起始时间

		if (flag) {
			calendar.add(Calendar.DATE, 1);
			defaultDate = calendar.getTime();
		}

		Date date22 = string2Date(date2String(defaultDate, "yyyy-MM-dd") + " " + time2, DEFAULT_PATTERN);// 终止时间

		Calendar scalendar = Calendar.getInstance();
		scalendar.setTime(date11);// 起始时间

		Calendar ecalendar = Calendar.getInstance();
		ecalendar.setTime(date22);// 终止时间

		Calendar calendarnow = Calendar.getInstance();

		if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 
	 * 判断时间是否在两个时间点之间，时间格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @author 
	 * @date 
	 * @version: V1.0
	 * 
	 * @param time1
	 *            起始时间串 (yyyy-MM-dd HH:mm:ss)
	 * @param time2
	 *            终止时间串 (yyyy-MM-dd HH:mm:ss)
	 * @return true-当前时间在tiem1与time2之间，false-当前时间不在tiem1与time2之间
	 */
	public static boolean checkTimeLong(String time1, String time2) {
		Date beginTime = DateOper.string2Date(time1, DATE_FORMAT1);
		Date endTime = DateOper.string2Date(time2, DATE_FORMAT1);

		// 起始时间
		Calendar scalendar = Calendar.getInstance();
		scalendar.setTime(beginTime);

		// 终止时间
		Calendar ecalendar = Calendar.getInstance();
		ecalendar.setTime(endTime);

		Calendar calendarnow = Calendar.getInstance();

		if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * 日期的算术操作，可以增加或者减少，可以某一部分进行操作 year--年 month-月 1-12 day-天 1-31 hour -小时 0-23
	 * minute 分钟 0-59 second 秒 0-59 millisecond 毫秒 显示格式，可以任意组合
	 * 
	 * G Era designator Text AD y Year Year 1996; 96 M Month in year Month July;
	 * Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year
	 * Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day
	 * in week Text Tuesday; Tue a Am/pm marker Text PM H Hour in day (0-23)
	 * Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h
	 * Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in
	 * minute Number 55 S Millisecond Number 978 z Time zone General time zone
	 * Pacific Standard Time; PST; GMT-08:00 Z Time zone RFC 822 time zone -0800
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param srcDate
	 * @param srcFormat
	 * @param destFormat
	 * @param operType
	 * @param operValue
	 * @return
	 */
	public static String evalTime(String srcDate, String srcFormatIn, String destFormatIn, String operTypeIn, int operValue) {
		String srcFormat = srcFormatIn, destFormat = destFormatIn, operType = operTypeIn;
		if (srcDate == null || "".equals(srcDate))
			return "";
		if (srcFormatIn == null || "".equals(srcFormatIn))
			srcFormat = "yyyy-MM-dd";
		if (destFormatIn == null || "".equals(destFormatIn))
			destFormat = "yyyy-MM-dd";
		if (operTypeIn == null || "".equals(operTypeIn))
			operType = "day";
		// Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("AST"));
		Calendar cal = Calendar.getInstance();
		Date currentTime = null;
		try {
			currentTime = (Date) new java.text.SimpleDateFormat(srcFormat).parse(srcDate);
		} catch (ParseException e) {
			logger.error("", e);
			currentTime = new Date();
		}
		cal.setTime(currentTime);
		if ("year".equals(operType)) {
			cal.add(Calendar.YEAR, operValue);
		} else if ("month".equals(operType)) {
			cal.add(Calendar.MONTH, operValue);
		} else if ("day".equals(operType)) {
			cal.add(Calendar.DAY_OF_MONTH, operValue);
		} else if ("hour".equals(operType)) {
			cal.add(Calendar.HOUR_OF_DAY, operValue);
		} else if ("minute".equals(operType)) {
			cal.add(Calendar.MINUTE, operValue);
		} else if ("second".equals(operType)) {
			cal.add(Calendar.SECOND, operValue);
		} else if ("millisecond".equals(operType)) {
			cal.add(Calendar.MILLISECOND, operValue);
		}
		String curDay = new java.text.SimpleDateFormat(destFormat).format(cal.getTime());
		return curDay;
	}

	/**
	 * 
	 * 日期相减，返回毫秒数差
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param date1
	 *            日期1
	 * @param date2
	 *            日期2
	 * @return 返回相减后的日期毫秒数，若入参为null，则返回0
	 */
	private static long diffDate(java.util.Date date1, java.util.Date date2) {
		if (date1 == null)
			return 0;
		if (date2 == null)
			return 0;

		return date1.getTime() - date2.getTime();
	}

	/**
	 * 获得当前时间 转换为时间戳类型
	 * 
	 * @author 王陶林 wangtlc@si-tech.com.cn
	 * @date 2012-11-10 上午11:11:26
	 * @version: V1.0
	 * 
	 * @return
	 * @throws ParseException
	 */
	public static Timestamp getCurTime2Timestamp() throws ParseException {
		Timestamp t = new Timestamp(System.currentTimeMillis());
		String d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(t);
		Timestamp.valueOf(d2);
		return t;
	}

	/**
	 * 日期格式转化 转换为时间戳类型
	 * 
	 * @param sdate
	 * @return
	 * @throws ParseException
	 */
	public static Timestamp string2Time(String sdate) throws ParseException {
//		java.sql.Timestamp d = null;
		java.util.Date d1 = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).parse(sdate);
		java.sql.Timestamp d = new java.sql.Timestamp(d1.getTime());
		String d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(d);
		Timestamp.valueOf(d2);
		return d;
	}

	/**
	 * 日期格式转化
	 * 
	 * @param srcDate
	 * @param srcFormat
	 * @param destFormat
	 * @return
	 */
	public static String dateFormat(Date srcDate, String destFormatIn) {
		String destFormat = destFormatIn;
		if (srcDate == null)
			return "";
		if (destFormatIn == null || "".equals(destFormatIn))
			destFormat = "yyyy-MM-dd";
		String curDay = new java.text.SimpleDateFormat(destFormat).format(srcDate);
		return curDay;
	}
	/**
	 * 字符串日期转化为java.util.Date
	 * @param srcDate
	 * @param srcFormat
	 * @return
	 */
	public static Date StringToDate(String srcDate, String srcFormatIn) {
		String srcFormat = srcFormatIn;
		if (srcDate == null || "".equals(srcDate))
			return new Date();
		if (srcFormatIn == null || "".equals(srcFormatIn))
			srcFormat = "yyyy-MM-dd";
		Calendar cal = Calendar.getInstance();
		Date currentTime = null;
		try {
			currentTime = (Date) new java.text.SimpleDateFormat(srcFormat).parse(srcDate);
		} catch (ParseException e) {
			logger.error("", e);
			currentTime = new Date();
		}
		cal.setTime(currentTime);
		return cal.getTime();
	}

	/**
	 * 将字符串格式化为‘yyyy-MM-dd HH:mm:ss’格式的java.util.Date
	 */
	public static Date StringToDate(String srcDate) {
		Date d = null;
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
		try {
			d = sdf.parse(srcDate);
		} catch (Exception e) {
			logger.error("", e);
		}
		return d;
	}

	/**
	 * 名称：dateToStr 功能：将指定的日期转换成字符串 输入：aDteValue: 要转换的日期; aFmtDate: 转换日期的格式,
	 * 默认为:"yyyy/MM/dd" 输出： 返回：转换之后的字符串
	 */
	public static String dateToStr(java.util.Date aDteValue, String aFmtDateIn) {
		String strRtn = null;
		String aFmtDate = aFmtDateIn;
		if (aFmtDateIn.length() == 0) {
			aFmtDate = "yyyy/MM/dd";
		}
		Format fmtDate = new SimpleDateFormat(aFmtDate);
		try {
			strRtn = fmtDate.format(aDteValue);
		} catch (Exception e) {
			logger.error("", e);
		}

		return strRtn;
	}

	/**
	 * 名称：strToDate 功能：将指定的字符串转换成日期 输入：aStrValue: 要转换的字符串; aFmtDate: 转换日期的格式,
	 * 默认为:"yyyy/MM/dd" aDteRtn: 转换后的日期 输出： 返回：TRUE: 是正确的日期格式; FALSE: 是错误的日期格式
	 */
	public static boolean strToDate(String aStrValue, String aFmtDateIn, java.util.Date aDteRtn) {
		String aFmtDate = aFmtDateIn;
		if (aFmtDateIn.length() == 0) {
			aFmtDate = "yyyy/MM/dd";
		}
		SimpleDateFormat fmtDate = new SimpleDateFormat(aFmtDate);
		try {
			aDteRtn.setTime(fmtDate.parse(aStrValue).getTime());
		} catch (Exception e) {
			logger.error("", e);
			return false;
		}
		return true;
	}

	/**
	 * 当前时间（字符串格式：yyyy-MM-dd HH:mm:ss）
	 * 
	 * @return
	 */
	public static String currentTime() {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sf.format(new Date());
	}

	/**
	 * java.util.Date转化为java.sql.Date
	 */
	public static java.sql.Date parseSqlDate(java.util.Date date) {
		if (date == null)
			return null;
		return new java.sql.Date(date.getTime());
	}

	/**
	 * java.util.Date转化为java.sql.Timestamp
	 */
	public static java.sql.Timestamp parseTimestamp(Date date, String format) {
		if (date == null)
			return null;
		long t = date.getTime();
		return new java.sql.Timestamp(t);
	}

	public static String getNextBeginDay() {
		Calendar cal = Calendar.getInstance();
//		@SuppressWarnings("unused")
//		String endDate_year = String.valueOf(cal.get(Calendar.YEAR));
//		@SuppressWarnings("unused")
//		String endDate_month;
//		if (cal.get(Calendar.MONTH) + 1 < 10) {
//			endDate_month = "0" + (cal.get(Calendar.MONTH) + 1);
//		} else {
//			endDate_month = String.valueOf(cal.get(Calendar.MONTH) + 1);
//		}
//		@SuppressWarnings("unused")
//		String endDate_day = String.valueOf(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		String effDate_year;
		String effDate_month;
		// 当月为12月时，下月应该为下一年的1月，下个月的年份应该加1
		if (cal.get(Calendar.MONTH) + 1 == 12) {
			effDate_year = String.valueOf(cal.get(Calendar.YEAR) + 1);
			effDate_month = "01";
		} else {
			effDate_year = String.valueOf(cal.get(Calendar.YEAR));
			// 当下月为小于10月时，应该为下月的前面加0，如02到09
			if (cal.get(Calendar.MONTH) + 2 < 10) {
				effDate_month = "0" + (cal.get(Calendar.MONTH) + 2);
			} else {
				effDate_month = String.valueOf(cal.get(Calendar.MONTH) + 2);
			}
		}

		String effDate = effDate_year + effDate_month + "01";// 更改后套餐生效日期
		return effDate;
	}

	/**
	 * 获得当前月份 转换为Long类型
	 * 
	 * @throws ParseException
	 */
	public static Long getCurMonthLong() throws ParseException {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		// t_log_echn_servinvoke.setTime_range(Long.valueOf(sdf.format(date)));
		return Long.parseLong(sdf.format(date));
	}

	/**
	 * 比较时间是否在这两个时间点之间 added by liuxw
	 * 
	 * @param time1
	 *            必须是yyyy-MM-dd HH:mm:ss格式
	 * @param time2
	 *            必须是yyyy-MM-dd HH:mm:ss格式
	 */
	public static boolean checkTime2(String time1, String time2) {
		Date beginTime = DateOper.StringToDate(time1);
		Date endTime = DateOper.StringToDate(time2);
		Calendar scalendar = Calendar.getInstance();
		scalendar.setTime(beginTime);
		System.out.println(scalendar);
		Calendar ecalendar = Calendar.getInstance();
		ecalendar.setTime(endTime);// 终止时间
		System.out.println(ecalendar);
		Calendar calendarnow = Calendar.getInstance();
		System.out.println(calendarnow);
		if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * 判断日期是否即将到期失效
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param date1
	 *            失效时间
	 * @param date2
	 *            当前时间
	 * @param delay
	 *            失效阀值（天）
	 * @return ture-已失效， false-未失效
	 */
	public static boolean toRemind(Date date1, Date date2, int delay) {

		Long time = diffDate(date1, date2);
		if (delay > time / (60 * 60 * 24 * 1000)) {
			return false;// 未失效
		} else {
			return true;// 即将失效，提醒
		}
	}

	/**
	 * 
	 * 根据操作类型更改日期
	 * 
	 * @author 
	 * @date
	 * @version: V1.0
	 * 
	 * @param date
	 *            源日期对象
	 * @param type
	 *            操作类型：date, year, month
	 * @param value
	 *            增加值(正数为增加，负数为减少)
	 * @return 更改后的日期对象
	 */
	public static Date changeDate(Date date, String type, int value) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		if ("month".equals(type)) {
			calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);
		} else if ("date".equals(type)) {
			calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);
		} else if ("year".endsWith(type)) {
			calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + value);
		}

		return calendar.getTime();
	}

	/**
	 * 
	 * 两日期相减，返回相差月数
	 * 
	 * @author 
	 * @date 
	 * @version: V1.0
	 * 
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @return 相差月数,startDate<endDate时，返回负数值，startDate>endDate时，返回正数值
	 */
	public static int divMonth(Date startDate, Date endDate) {
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);

		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);

		int yearDiv = startCalendar.get(Calendar.YEAR) - endCalendar.get(Calendar.YEAR);

		int monthDiv = startCalendar.get(Calendar.MONTH) - endCalendar.get(Calendar.MONTH);

		return monthDiv + yearDiv * 12;
	}

	/**
	 * 查询除本月外的几个月（入参设置 maxMonth=查询最大月数）
	 */
	public static List<Date> queryBillCycle(int maxMonth) {
		List<Date> billingCycleList = new ArrayList<Date>();
		String currentYear = new SimpleDateFormat("yyyy", Locale.getDefault()).format(new Date());
		String currentMonth = new SimpleDateFormat("MM", Locale.getDefault()).format(new Date());
		int mon = Integer.parseInt(currentMonth);
		// int maxMonth=6;//可访问月数

		if (mon > 7) {
			for (int i = 1; i <= maxMonth; i++) {
				String srcDate = currentYear + "0" + String.valueOf(mon - i);
				Date d = DateOper.StringToDate(srcDate, "yyyyMM");
				billingCycleList.add(d);
			}
		} else {
			int temp = 0;
			for (int i = mon - 1; i > 0; i--) {
				String srcDate = currentYear + "0" + String.valueOf(i);
				Date d = DateOper.StringToDate(srcDate, "yyyyMM");
				billingCycleList.add(d);
				temp++;
			}
			for (int i = 12; i > (12 - maxMonth + temp); i--) {
				String srcDate;
				if (i == 12 || i == 11 || i == 10) {
					srcDate = (Integer.parseInt(currentYear) - 1) + String.valueOf(i);
				} else {
					srcDate = (Integer.parseInt(currentYear) - 1) + "0" + String.valueOf(i);
				}
				Date d = DateOper.StringToDate(srcDate, "yyyyMM");
				billingCycleList.add(d);
			}
		}
		return billingCycleList;
	}

	/**
	 * 查询几个月（入参设置 maxMonth=查询最大月数）
	 */
	public static List<Date> queryBillDate(int maxMonth) {
		List<Date> billingCycleList = new ArrayList<Date>();
		String currentYear = new SimpleDateFormat("yyyy", Locale.getDefault()).format(new Date());
		String currentMonth = new SimpleDateFormat("MM", Locale.getDefault()).format(new Date());
		int mon = Integer.parseInt(currentMonth);
		// int maxMonth=6;//可访问月数

		if (mon > 7) {
			for (int i = 0; i <= maxMonth; i++) {
				String srcDate = currentYear + "0" + String.valueOf(mon - i);
				Date d = DateOper.StringToDate(srcDate, "yyyyMM");
				billingCycleList.add(d);
			}
		} else {
			int temp = 0;
			for (int i = mon; i > 0; i--) {
				String srcDate = currentYear + "0" + String.valueOf(i);
				Date d = DateOper.StringToDate(srcDate, "yyyyMM");
				billingCycleList.add(d);
				temp++;
			}
			for (int i = 12; i > (12 - maxMonth + temp); i--) {
				String srcDate;
				if (i == 12 || i == 11 || i == 10) {
					srcDate = (Integer.parseInt(currentYear) - 1) + String.valueOf(i);
				} else {
					srcDate = (Integer.parseInt(currentYear) - 1) + "0" + String.valueOf(i);
				}
				Date d = DateOper.StringToDate(srcDate, "yyyyMM");
				billingCycleList.add(d);
			}
		}
		return billingCycleList;
	}

    /** 判断当前时间是否在月末最后一天18：00----次日8：00之间
	 * 
	 * @param begin
	 *            倒数多少天 例如月末最后一天 begin=1 月末倒数第二天 begin=2 默认为月末最后一天
	 * @param end
	 *            整数多少天 例如月初第一天 end=1 月初第二天 end=2 默认为月初第一天
	 * @param time1
	 *            月末时间设置 格式为 HH:mm:ss 默认为0点
	 * @param time2
	 *            月初时间设置 格式为 HH:mm:ss 默认为0点
	 * @return
	 */
	public static boolean checkTime(int beginIn, int endIn, String time1In, String time2In) {
		String time1 = time1In, time2 = time2In;int begin = beginIn, end = endIn;
		if (time1In == null) {
			time1 = "00:00:00";
		}
		if (time2In == null) {
			time2 = "00:00:00";
		}
		if (beginIn == 0) {
			begin = 1;
		}
		if (endIn == 0) {
			end = 1;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal1 = Calendar.getInstance();
		final int lastDay = cal1.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal1.set(Calendar.DAY_OF_MONTH, lastDay - (begin - 1)); // 第一个月末
		String firstDate = format.format(cal1.getTime()) + " " + time1.trim();

		Calendar cal2 = Calendar.getInstance();
		cal2.add(Calendar.MONTH, 1);
		cal2.set(Calendar.DAY_OF_MONTH, end);// 第二个月初
		String lastDate = format.format(cal2.getTime()) + " " + time2.trim();

		Date date11 = DateOper.StringToDate(firstDate, "yyyy-MM-dd HH:mm:ss");// 起始时间
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, 1);
		Date date22 = DateOper.StringToDate(lastDate, "yyyy-MM-dd HH:mm:ss");// 终止时间
		Calendar scalendar = Calendar.getInstance();
		scalendar.setTime(date11);// 起始时间
		Calendar ecalendar = Calendar.getInstance();
		ecalendar.setTime(date22);// 终止时间
		Calendar calendarnow = Calendar.getInstance();
		if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 当前年月（字符串格式：yyyyMM）
	 * 
	 * @return
	 */
	public static String getCurrentYearMonth() {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
		return sf.format(new Date());
	}
	public static String date2Str(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	public static synchronized String GenDateString(String dateFmt){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(dateFmt);
		return sdf.format(date);
	}	
	
	/**
	 * @param 要转换的毫秒数
	 * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
	 * @author fy.zhang
	 */
	public static String formatDuring(long mss) {
		long days = mss / (1000 * 60 * 60 * 24);
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
	}
	public static String formatDuring2(long mss) {
		return  mss+"(ms)";
	}

	public static Calendar chgString2Calendar(String targetTime) {
		Date beginTime = DateOper.string2Date(targetTime, DATE_FORMAT1);
		Calendar scalendar = Calendar.getInstance();
		scalendar.setTime(beginTime);
		return scalendar;
	}
	
	/**
	 * 判断时间是否在两个时间点之间
	 * 
	 * @param beginTime
	 *            起始时间Calendar
	 * @param endTime
	 *            终止时间Calendar
	 * @return true-当前时间在beginTime与endTime之间，false-当前时间不在beginTime与endTime之间
	 */
	public static boolean checkTimeLong(Calendar beginTime, Calendar endTime) {
		Calendar calendarnow = Calendar.getInstance();
		if (calendarnow.after(beginTime) && calendarnow.before(endTime)) {
			return true;
		} else {
			return false;
		}
	}
}

